package com.talkyun.utils.crypt;

public class KeyStringUtils {
    public static int LINE_CHAR_NUM = 64;

    public static String wrapToLines(String key) {
        return wrapToLines(key, LINE_CHAR_NUM);
    }

    public static String wrapToLines(String key, int lineCharNum) {
        StringBuilder sb = new StringBuilder();
        int num = 0;
        for (char c : key.toCharArray()) {
            if (num++ % lineCharNum == 0) {
                if (sb.length() != 0) {
                    sb.append("\r\n");
                }
            }
            sb.append(c);
        }
        return sb.toString();
    }

    public static String cleanKeyString(String key) {
        if (key == null || key.trim().length() == 0) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        char[] buf = key.toCharArray();

        for (char c : buf) {
            if (isKeyChar(c)) {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static boolean isKeyChar(char c) {
        // TODO '+' '-' '=' ( base 64 char  )
        return Character.isLetter(c) || Character.isDigit(c);
    }

    public static void main(String[] args) {
        String key = "                0FC5BNJIxxHNxxx7N4TRBNJDZUQRZLSK4WE2A\n" +
                "                7MEAWFxxxxxYDGB46ODVV7YETF5PLDEUUTS7F2HMTJP5XVOIH7OMR3E\n" +
                "                B76XFYFLEVPWZBIGSWB6CZ5ZNJJHFXC6HTHWTxxxS47DWRBUBJF3SKHVBIEF\n" +
                "                GHZYIJQTLZ7Y7VPCxxxMOFM2FJFMYP6D2DXC3ALCF3L4WMO5YDBJPVMLFIFOFQDY5\n" +
                "                GXRVYNKGOML5PPKQ";

        System.out.println(cleanKeyString(key));
    }

}
